package org.itboys.mobile.controller.web.meal;

import com.wordnik.swagger.annotations.Api;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.conmon.Finals;
import org.itboys.mdl.entity.mongo.meal.MdlMealProduct;
import org.itboys.mdl.service.mongo.meal.MdlMealService;
import org.itboys.mobile.service.mongo.web.meal.MdlMealProductService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017/12/1
 * 描述：迈迪龙_套餐商品
 */
@Controller
@RequestMapping("/mdl/web/meal/product")
public class MdlMealProductController extends BaseController{
    @Autowired
    private MdlMealProductService mealProductService;
    @Autowired
    private MdlMealService mealService;
    /**
     * 套餐商品主页
     * @param request
     * @param model
     * @param mealId
     * @return
     */
    @RequestMapping("/index")
    public String product_index(HttpServletRequest request,
                                Model model,
                                @RequestParam(value="mealId",required=true)Long mealId){

        model.addAttribute("mealId", mealId);
        return "/maidilong/meal/product/index";
    }

    /**
     * 后台分页获取套餐商品
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void product_select(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="mealId",required=true)Long mealId){
        try{
            PageResult<MdlMealProduct> result=mealProductService.selectMealProduct(request,mealId);

            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 套餐商品详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String product_input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="mealId",required=false)Long mealId,
            Model model){
        MdlMealProduct mealProduct =mealProductService.findOne(id,mealId);
        model.addAttribute("mealProduct", mealProduct);
        return "/maidilong/meal/product/input";
    }

    /**
     * 新增或保存  套餐商品
     * @param request
     * @param response
     * @param mealProduct
     */
    @RequestMapping("/save")
    public void product_save(HttpServletRequest request,
                             HttpServletResponse response,
                             @ModelAttribute MdlMealProduct mealProduct){
        try{
            mealProductService.saveMealProduct(mealProduct);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/mdl/web/meal/product/save------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 删除套餐商品
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void product_delete(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="id",required=true)Long id){
        try{
            mealProductService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/mdl/web/meal/product/delete------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
